package com.zkingsoft.actions.admin;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.config.Dictionary;
import com.zkingsoft.model.Img;
import com.zkingsoft.model.PaginationVO;
import com.zkingsoft.model.ShowElement;
import com.zkingsoft.plugin.util.StringUtils;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.pojo.ShowElementImg;
import com.zkingsoft.services.ImgService;
import com.zkingsoft.services.ShowElementService;

/**
 * @project MatrixCms
 * @package com.zkingsoft.actions.admin
 * @class ShowElementController.java
 * @author Matrix-J E-mail:<935090232@qq.com>
 * @date 2016年4月21日10:31
 * @description 系统按钮管理
 */
@Controller
@RequestMapping(value = "/admin/pruduct")
public class PruductController extends BaseController {

	private static final Logger log = Logger.getLogger(PruductController.class);

	@Resource
	private ShowElementService currentService;
	@Resource
	private ImgService imgService;

	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(HttpServletRequest request, ShowElement pruduct, PaginationVO pageVo) {
		log.info("按钮列表...");
		pruduct.setType(Dictionary.ELEMENT_TYPE_PRODUCT);
		pruduct.setCompanyId(getMe(request).getCompanyId());
		return showList(currentService, pruduct, pageVo);
	}

	@RequestMapping(value = "/all")
	public @ResponseBody AjaxResult all() {
		ShowElement showElement = new ShowElement();
		showElement.setType(Dictionary.ELEMENT_TYPE_PRODUCT);
		return findByModel(currentService, showElement);
	}

	@RequestMapping(value = "/changeShow")
	public @ResponseBody AjaxResult changeShow(Integer flage, String keys, HttpServletRequest request) {
		ShowElement pruduct = new ShowElement();
		pruduct.setIsShow(flage);
		pruduct.setId(Long.parseLong(keys));
		currentService.modify(pruduct);
		String msg = "";
		if (flage == 1) {
			msg = "发布成功";
		} else if (flage == 2) {
			msg = "产品已经撤回";
		}
		return new AjaxResult(AjaxResult.STATUS_OK, null, msg);
	}

	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(ShowElementImg pruduct, HttpServletRequest request) {
		String name = "产品";
		if (pruduct.getId() != null) {
			int i = currentService.modifyPrudct(pruduct);
			if (i != 0) {
				return new AjaxResult(AjaxResult.STATUS_OK, null, name + "修改成功");
			} else {
				return new AjaxResult(AjaxResult.STATUS_ERR, null, name + "修改失败");
			}

		} else {
			pruduct.setType(Dictionary.ELEMENT_TYPE_PRODUCT);
			pruduct.setCompanyId(getMe(request).getCompanyId());
			pruduct.setTime(new Date());
			int i = currentService.addPrudct(pruduct);
			if (i != 0) {
				return new AjaxResult(AjaxResult.STATUS_OK, null, name + "添加成功");
			} else {
				return new AjaxResult(AjaxResult.STATUS_ERR, null, name + "添加失败");
			}
		}
	}

	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		List<Long> ids = StringUtils.strToCollToLong(keys, ",");
		int i = currentService.removePruduct(ids);
		if (i != 0) {
			return new AjaxResult(AjaxResult.STATUS_OK, null, "成功删除" + i + "条数据");
		} else {
			return new AjaxResult(AjaxResult.STATUS_ERR, null, "删除失败");
		}
	}

	@RequestMapping(value = "/editForm")
	public String editForm(Long id, HttpServletRequest request) {
		if (id != null) {
			ShowElement showElement = currentService.findById(id);
			Img img=new Img();
			img.setCompanyId(showElement.getCompanyId());
			img.setTargetId(showElement.getId());
			img.setType(Dictionary.ELEMENT_TYPE_PRODUCT);
			List<Img> imgList=imgService.findByModel(img);
			request.setAttribute("imgList", imgList);
			request.setAttribute("imgLength", imgList.size());
			request.setAttribute("obj", showElement);
		}
		return "admin/pruduct-form";
	}

}